###########################################################
import tool.sputil
import pdfkit

# resp = requests.get('')
# soup = BeautifulSoup(resp.content, "html5lib")
sprint =  tool.sputil.sprint;
getsoup = tool.sputil.getsoup;

def save_pdf(htmls):
    """
    把所有html文件转换成pdf文件
    """
    options = {
        'page-size': 'Letter',
        'encoding': "UTF-8",
        'custom-header': [
            ('Accept-Encoding', 'gzip')
        ]
    }

    pdfkit.from_file(htmls, 'lxf', options=options)






def getUrlList(url):
    urls = []
    soup = getsoup(url)
    list = soup.find_all(class_="uk-nav uk-nav-side")[1]
    for li in list.find_all("li"):
        liurl = "http://www.liaoxuefeng.com" + li.a.get('href')
        urls.append({'title':li.a.text,'url':liurl})

    return urls

urllist = getUrlList('http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431752945034eb82ac80a3e64b9bb4929b16eeed1eb9000')

def getHtmlText(url):
    soup = getsoup(url)
    body = soup.find_all(class_="x-wiki-content")[0]
    return body

for index, item in enumerate(urllist):
    html = str(getHtmlText(item['url']))
    title = str(index)+item['title']+'.html'
    title = title.replace('/','+')
    # print('title=' + title)
    # print('body=' + html)
    # with open(title, 'w',encoding='UTF-8') as f:
    #     if isinstance(html,str):
    #         print(title)
    #     f.write(html)
    #     f.close()

# print('h1 =======================',urllist)


# print('resp.text =======================',soup.body.text)



